package com.isoftstone.merchant.service.notice.impl;


import com.isoftstone.common.Page;
import com.isoftstone.merchant.dao.feeUser.MFeeUserMapper;
import com.isoftstone.merchant.dao.notify.BNotifyDetailMapper;
import com.isoftstone.merchant.dao.notify.BNotifyMapper;
import com.isoftstone.merchant.entity.feeUser.MFeeUser;
import com.isoftstone.merchant.entity.notify.BNotify;
import com.isoftstone.merchant.entity.notify.BNotifyDetail;
import com.isoftstone.merchant.service.notice.INotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class NotifyService implements INotifyService {

    @Autowired
    BNotifyMapper notifyMapper;

    @Autowired
    BNotifyDetailMapper notifyDetailMapper;

    @Autowired
    MFeeUserMapper mFeeUserMapper;


    @Override
    public List<BNotify> findnotifylistPage(Page page) throws Exception {
        return notifyMapper.findnotifylistPage(page);
    }

    @Override
    public List<BNotify> findnotifylis() throws Exception {
        return notifyMapper.findnotifylis();
    }

    public void withdrawNotify(Long notifySeq) {
        notifyMapper.withdrawNotify(notifySeq);
    }

    @Override
    public void insertNotify(String[] treeDemoList, BNotifyDetail bNotifyDetail, BNotify bNotify) {
        //新增通知并返回通知id
        long notifyId = notifyMapper.insertNotifyAndGetId(bNotify);
        System.out.println("新增数据的id = " + bNotify.getNotifySeq());
        try {
            if (treeDemoList != null && treeDemoList.length > 0) {
                for (int i = 0; i < treeDemoList.length; i++) {
                    BNotifyDetail bNotifyDetail_1 = new BNotifyDetail();
                    bNotifyDetail_1.setNotify_id(bNotify.getNotifySeq());
                    bNotifyDetail_1.setUser_type(bNotifyDetail.getUser_type());
                    bNotifyDetail_1.setIs_read(bNotifyDetail.getIs_read());
                    //根据dept_id查询fee_user
                    List<MFeeUser> feeUsers = mFeeUserMapper.getFeeUserByDeptId(treeDemoList[i]);
                    for (MFeeUser feeUser : feeUsers) {
                        bNotifyDetail_1.setUser_id(feeUser.getFeeUserId());
                        notifyDetailMapper.insertNotifyDetail(bNotifyDetail_1);
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insertNotify(String[] mUsers, BNotifyDetail bNotifyDetail, String[] feeUsers, BNotifyDetail bNotifyDetail2, BNotify bNotify) {
        //新增通知并返回通知id
        long notifyId = notifyMapper.insertNotifyAndGetId(bNotify);
        System.out.println("新增数据的id = " + bNotify.getNotifySeq());
        if (mUsers != null && mUsers.length > 0) {
            for (int i = 0; i < mUsers.length; i++) {
                BNotifyDetail bNotifyDetail_1 = new BNotifyDetail();
                bNotifyDetail_1.setNotify_id(bNotify.getNotifySeq());
                bNotifyDetail_1.setUser_id(mUsers[i]);
                bNotifyDetail_1.setUser_type(bNotifyDetail.getUser_type());
                bNotifyDetail_1.setIs_read(bNotifyDetail.getIs_read());

                notifyDetailMapper.insertNotifyDetail(bNotifyDetail_1);
            }
        }
        if (feeUsers != null && feeUsers.length > 0) {
            for (int i = 0; i < feeUsers.length; i++) {
                BNotifyDetail bNotifyDetail_1 = new BNotifyDetail();
                bNotifyDetail_1.setNotify_id(bNotify.getNotifySeq());
                bNotifyDetail_1.setUser_id(feeUsers[i]);
                bNotifyDetail_1.setUser_type(bNotifyDetail2.getUser_type());
                bNotifyDetail_1.setIs_read(bNotifyDetail2.getIs_read());

                notifyDetailMapper.insertNotifyDetail(bNotifyDetail_1);
            }
        }
    }

    //手机端获取未读通知数量
    public List<BNotify> getNReadNotifyNum(String read, String status, String openAccount) throws Exception {
        return notifyMapper.getNReadNotifyNum(read, status, openAccount);
    }

    //手机端获取未读通知分类明细
    public List<BNotify> getNReadNotifyList(String read, String type, String status, String openAccount) throws Exception {
        return notifyMapper.getNReadNotifyList(read, type, status, openAccount);
    }

    //手机端获取已读通知分类明细
    public List<BNotify> getReadNotifyList(String read, String type, String status, String openAccount) throws Exception {
        return notifyMapper.getReadNotifyList(read, type, status, openAccount);
    }

    //手机端获取普通/签约通知
    public List<BNotify> getNotifyListOne(String read, String type, String status, String openAccount) throws Exception {
        return notifyMapper.getNotifyListOne(read, type, status, openAccount);
    }

    //手机端获取缴费通知
    public List<BNotify> getNotifyListTwo(String read, String type, String status, String openAccount) throws Exception {
        return notifyMapper.getNotifyListTwo(read, type, status, openAccount);
    }

    //修改消息读取状态
    public void updateNotifyRead(String read, String notifyId, String userId) throws Exception {
        notifyMapper.updateNotifyRead(read, notifyId, userId);
    }
    //修改消息读取状态
    public void updateNotifyReadById(String read, String notifyId, String openAccount)throws Exception{
        notifyMapper.updateNotifyReadById(read, notifyId, openAccount);
    }

    @Override
    public void reminderNotify(BNotifyDetail bNotifyDetail, BNotify bNotify, String feeUserId) {
        //新增通知并返回通知id
        long notifyId = notifyMapper.insertNotifyAndGetId(bNotify);
        System.out.println("新增数据的id = " + bNotify.getNotifySeq());
        BNotifyDetail bNotifyDetail_1 = new BNotifyDetail();
        bNotifyDetail_1.setNotify_id(bNotify.getNotifySeq());
        bNotifyDetail_1.setUser_id(feeUserId);
        bNotifyDetail_1.setPay_id(bNotifyDetail.getPay_id());
        bNotifyDetail_1.setUser_type(bNotifyDetail.getUser_type());
        bNotifyDetail_1.setIs_read(bNotifyDetail.getIs_read());

        notifyDetailMapper.insertNotifyDetail(bNotifyDetail_1);
    }


    @Override
    @Transactional
    public int saveNotify(BNotify bNotify) throws Exception {
        return notifyMapper.insertSelective(bNotify);
    }

    @Override
    @Transactional
    public void saveNotifyDetail(BNotifyDetail bNotifyDetail) {
        notifyDetailMapper.insertNotifyDetail(bNotifyDetail);
    }

    @Override
    public List<BNotify> findMervhantNotifylistPage(Page page) throws Exception {
        return notifyMapper.findMervhantNotifylistPage(page);
    }
    //商户端获得银行通知
    @Override
    public List<BNotify> findBankNotifylistPage(Page page) throws Exception {
        return notifyMapper.findBankNotifylistPage(page);
    }

    @Override
    public List<BNotifyDetail> getNotifyByPayIdAndfeeUserID(Map<String, Object> map) throws Exception {
        return notifyDetailMapper.getNotifyByPayIdAndfeeUserID(map);
    }

    @Override
    public void deleteNotify(Long notify_id) throws Exception {
        notifyMapper.deletenotify(notify_id);
    }

    @Override
    public void deletenotifyFromDetail(Map<String, Object> map) throws Exception {
        notifyDetailMapper.deletenotifyFromDetail(map);
    }
}
